package com.yuandian.customer.perception.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.customer_perception.po.OperatorOverviewParams;
import com.yuandian.api.customer_perception.vo.*;
import com.yuandian.api.management.feign.RemoteBusinessHallService;
import com.yuandian.bpm.common.core.constant.CommonConstants;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.customer.perception.mapper.OperatorMonitorMapper;
import com.yuandian.customer.perception.service.IOperatorOverviewService;
import com.yuandian.po.MultipleTableInfo;
import com.yuandian.utils.PageDataInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 操作员监控总览实现类
 * <p>
 *
 * @author lijunhang
 * @date 2021/8/16
 * @since 1.0.0
 */
@Service
@Slf4j
public class OperatorOverviewServiceImpl implements IOperatorOverviewService {


    @Autowired
    private OperatorMonitorMapper operatorMonitorMapper;


    @Autowired
    private RemoteBusinessHallService remoteBusinessHallService;
    /**
     * 操作员总览列表
     *
     * @param params 请求参数
     * @param tableInfos
     * @return 操作员总览列表数据
     */
    @Override
    public PageDataInfo operatorOverviewList(OperatorOverviewParams params, List<MultipleTableInfo> tableInfos) {
        Page page = new Page<>(params.getPageNum(), params.getPageSize());
        List<OperatorOverviewVo> list = operatorMonitorMapper.operatorOverviewList(params, page, tableInfos);
        list.forEach(operatorOverviewVo -> {
            if (operatorOverviewVo.getSuccessRate() == null) {
                operatorOverviewVo.setSuccessRate("0.00");
            }
            if (operatorOverviewVo.getAvgAllTime() == null) {
                operatorOverviewVo.setAvgAllTime("0.00");
            }
            if (operatorOverviewVo.getAvgVisitTime() == null) {
                operatorOverviewVo.setAvgVisitTime("0.00");
            }
            if (("0.00").equals(operatorOverviewVo.getAvgVisitTime())) {
                operatorOverviewVo.setAvgVisitTime("-");
            }
            if (operatorOverviewVo.getAvgResponseTime() == null) {
                operatorOverviewVo.setAvgResponseTime("0.00");
            }
        });
        return new PageDataInfo(page, list);
    }


    /**
     * 操作员总览列表下载
     *
     * @param params 请求参数
     * @param tableInfos
     * @return 操作员总览列表数据
     */
    @Override
    public List<OperatorOverviewVo> operatorOverviewListExport(OperatorOverviewParams params, List<MultipleTableInfo> tableInfos) {
        List<OperatorOverviewVo> list = operatorMonitorMapper.operatorOverviewList(params, tableInfos);
        list.forEach(operatorOverviewVo -> {
            if (operatorOverviewVo.getSuccessRate() == null) {
                operatorOverviewVo.setSuccessRate("0.00");
            }
            if (operatorOverviewVo.getAvgAllTime() == null) {
                operatorOverviewVo.setAvgAllTime("0.00");
            }
            if (operatorOverviewVo.getAvgVisitTime() == null) {
                operatorOverviewVo.setAvgVisitTime("0.00");
            }
            if (("0.00").equals(operatorOverviewVo.getAvgVisitTime())) {
                operatorOverviewVo.setAvgVisitTime("-");
            }
            if (operatorOverviewVo.getAvgResponseTime() == null) {
                operatorOverviewVo.setAvgResponseTime("0.00");
            }
            //处理数据,分情况回填未知
            if (operatorOverviewVo.getAreaCode() == null || "".equals(operatorOverviewVo.getAreaCode())) {
                operatorOverviewVo.setAreaCode("-");
            }
            if (operatorOverviewVo.getAreaName() == null || "".equals(operatorOverviewVo.getAreaName())) {
                if (Objects.equals(operatorOverviewVo.getAreaCode(), "-")) {
                    operatorOverviewVo.setAreaName("-");
                } else {
                    operatorOverviewVo.setAreaName("未知");
                }
            }
            if (operatorOverviewVo.getBusinessHallId() == null || "".equals(operatorOverviewVo.getBusinessHallId())) {
                operatorOverviewVo.setBusinessHallId("-");
            }
            if (operatorOverviewVo.getBusinessHallName() == null || "".equals(operatorOverviewVo.getBusinessHallName())) {
                if (Objects.equals(operatorOverviewVo.getBusinessHallId(), "-")) {
                    operatorOverviewVo.setBusinessHallName("-");
                } else {
                    operatorOverviewVo.setBusinessHallName("未知");
                }
            }
            if (operatorOverviewVo.getOperatorId() == null || "".equals(operatorOverviewVo.getOperatorId())) {
                operatorOverviewVo.setOperatorId("-");
            }
            if (operatorOverviewVo.getOperatorName() == null || "".equals(operatorOverviewVo.getOperatorName())) {
                if (Objects.equals(operatorOverviewVo.getOperatorId(), "-")) {
                    operatorOverviewVo.setOperatorName("-");
                } else {
                    operatorOverviewVo.setOperatorName("未知");
                }
            }
        });
        return list;
    }


    /**
     * 查询指定地区下的营业厅
     *
     * @param params 请求参数
     * @return 查询指定地区下的营业厅列表
     */
    @Override
    public List<SearchOptionVo> queryBusinessHall(OperatorOverviewParams params) {
        R<List<SearchOptionVo>> listR = remoteBusinessHallService.queryBusinessHall(params);
        if (listR.getCode() == CommonConstants.FAIL){
            log.error("远程调用配置系统获取营业厅名称失败,错误：{}",listR.getMsg());
        }
		return listR.getData();
    }


    /**
     * 查询指定地区下的营业厅
     *
     * @param params 请求参数
     * @return 查询指定地区下的营业厅列表
     */
    @Override
    public PageDataInfo queryBusinessHall2(OperatorOverviewParams params) {
        Page page = new Page<>(params.getPageNum(), params.getPageSize());
//		List<SearchOptionVo> list = operatorMonitorMapper.queryBusinessHall2(params, page);	TODO 远程调用
		List<SearchOptionVo> list = new ArrayList<>();
        return new PageDataInfo(page, list);
    }


    /**
     * 查询操作员当前在线人数
     *
     * @param params 请求参数
     * @param tableInfos
     * @return 查询操作员当前在线人数
     */
    @Override
    public int operatorOnlineNum(OperatorOverviewParams params, List<MultipleTableInfo> tableInfos) {
        return operatorMonitorMapper.operatorOnlineNum(params, tableInfos);
    }


    /**
     * 操作员受理量top5
     *
     * @param params 请求参数
     * @param tableInfos
     * @return 操作员受理量top5数据
     */
    @Override
    public List<OperatorCompleteTopVo> operatorCompleteTop5(OperatorOverviewParams params, List<String> operatorIds, List<MultipleTableInfo> tableInfos) {
        return operatorMonitorMapper.operatorCompleteTop5(params, operatorIds, tableInfos);
    }


    /**
     * 人工操作平均耗时Top5
     *
     * @param params 请求参数
     * @param tableInfos
     * @return 人工操作平均耗时Top5数据
     */
    @Override
    public List<OperatorCompleteTimeTopVo> operatorCompleteTimeTop5(OperatorOverviewParams params, List<MultipleTableInfo> tableInfos) {
        return operatorMonitorMapper.operatorCompleteTimeTop5(params, tableInfos);
    }


    /**
     * 操作员活跃数量
     *
     * @param params 请求参数
     * @param tableInfos
     * @return 操作员活跃数量数据
     */
    @Override
    public List<OperatorActiveCountVo> operatorActiveCount(OperatorOverviewParams params, List<MultipleTableInfo> tableInfos) {
        return operatorMonitorMapper.operatorActiveCount(params, tableInfos);
    }


    /**
     * 操作员违规告警提示列表下载
     *
     * @param params 请求参数
     * @return 操作员违规告警提示列表数据
     */
    @Override
    public List<OperatorViolationVo> operatorViolationListExport(OperatorOverviewParams params) {
        return operatorMonitorMapper.operatorViolationList(params);
    }


    /**
     * 查询指定业务员近一月的违规业务最多的业务名称
     *
     * @param params 请求参数
     * @return 指定业务员近一月的违规业务最多的业务名称
     */
    @Override
    public List<OperatorViolationVo> operatorBusinessName(OperatorOverviewParams params, List<String> operators) {
        return operatorMonitorMapper.operatorBusinessName(params, operators);
    }


    /**
     * 操作员总览-回填指定用户最近上线时间
     *
     * @param params
     * @param operators
     * @param tableInfos
     * @return
     */
    @Override
    public List<OperatorViolationVo> queryOnlineTimeByOperatorId(OperatorOverviewParams params, List<String> operators, List<MultipleTableInfo> tableInfos) {
        return operatorMonitorMapper.queryOnlineTimeByOperatorId(params, operators, tableInfos);
    }
}
